/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : my_qna_list.js                            
*  Description  : 마이페이지 qna
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-07-06  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	// 질문
	function clsQuestion()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsQuestion.prototype = {
		_init: function(){
			var $layer = $("div#container table.view01");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			this.setInfo({
				"제목": ""
				, "대분류": ""
				, "소분류": ""
				, "처리현황": ""
				, "등록일": ""
				, "내용": ""
				, "첨부파일": {}
			});
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			var c = (this.infoData["답변여부"] == "Y") ? "state_ok":"state_acc";
			
			$layer.find("thead th").html("<span>Q.</span>" + this.infoData["제목"]);
			$layer.find("dd.quest").html("<a href='" + _gf.filePath2Url(_util.nvl(this.infoData["첨부파일"]["파일명"])) + "' id='idPermissionLink'>" + _util.nvl(this.infoData["첨부파일"]["원본파일명"]) + "</a>");
			$layer.find("dd.quest").html("<span>[" + this.infoData["대분류"]+ "]</span><strong>" + this.infoData["소분류"]+ "</strong>");
			$layer.find("tbody>tr:eq(0)>td:eq(1)>dl>dd:eq(1)").attr("class", c).html(this.infoData["처리현황"]);
			$layer.find("tbody>tr:eq(0)>td:eq(1)>dl>dd:eq(2)").html(this.infoData["등록일"]);
			$layer.find("div.text").html(this.infoData["내용"]);
		}
	}
	
	// 답변
	function clsAnswer()
	{
		this._HTMLDom = null;
		this.infoData = null;
		
		this._init();
	}
	
	clsAnswer.prototype = {
		_init: function(){
			var $layer = $("div#container ul.qna_reply");
			
			this._HTMLDom = {
				"$layer": $layer
			}
			
			$layer.hide();
			
			this.setInfo({
				"답변등록일": ""
				, "답변": ""
			});
		}
		, setInfo: function(thisData){
			this.infoData = thisData;
			if(this.infoData["답변여부"] == "Y") this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			
			this.show();
			$layer.find("li:eq(0)").html("<span>A.</span>" + this.infoData["답변"]);
			$layer.find("li:eq(1)").html(this.infoData["답변등록일"]);
		}
		, show: function(){
			this._HTMLDom["$layer"].show(300);
		}
	}
	
	// 서버통신
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		, getContents: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "getDetailQna";
			var options = {
				"qna_seq": arg["질문코드"]
				, "modpage_yn": "N"
			}
			_gf.ajax({
				"sid": "QA001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
					, "결과값": {}
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					var obj = items;
					var reg_date = _util.nvl(obj["reg__date"])+"";
					var mod_date = _util.nvl(obj["mod__date"])+"";
					
					var fileList = {};
					if(obj["FileBean"]){
						var f = obj["FileBean"][0]["items"];
						
						fileList = {
							"파일코드": f["file__seq"]
							, "섬네일": "_NOT"
							, "파일명": f["path"] + f["save__file__name"]
							, "원본파일명": f["orgn__file__name"]
						}
					}
					
					res["결과값"] = {
						"질문코드": _util.nvl(obj["qna__seq"])
						, "대분류코드": _util.nvl(obj["qna__type"])
						, "대분류": _util.nvl(obj["qna__name"])
						, "소분류코드": _util.nvl(obj["qna__detail__type"])
						, "소분류": _util.nvl(obj["qna__detail__name"])
						, "제목": _util.nvl(obj["title"])
						, "내용": _util.enter2br(_util.nvl(obj["contents"]))
						, "답변": _util.nvl(obj["reply"])
						, "답변여부": _util.nvl(obj["status"])
						, "처리현황": _util.nvl(obj["status"]) == "Y" ? "답변완료": "문의접수"
						, "등록일": reg_date.substrEx(0, 4) + "-" + reg_date.substrEx(4, 2) + "-" + reg_date.substrEx(6, 2)
						, "답변등록일": mod_date.substrEx(0, 4) + "-" + mod_date.substrEx(4, 2) + "-" + mod_date.substrEx(6, 2)
						, "첨부파일": fileList
					}
				}
				
				document.objControl.finishContents(res);
			});
		}
		// 삭제
		, articleDelete: function(arg){
			var thisClass = this;
			
			//ajax
			var serviceName = "deleteQna";
			var options = {
				"qna_seq": arg["질문코드"]
			}
			_gf.ajax({
				"sid": "QA001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": _gf.getResultMsg(ret)
				}
				
				document.objControl.finishArticleDelete(res);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objQuestion = null;
		this.objAnswer = null;
		
		this.q_seq = null;
		this.q_page = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			this._preload();
			
			this.objComm = new clsComm();
			this.objQuestion = new clsQuestion();
			this.objAnswer = new clsAnswer();
			
			this._beforeStart();
			this._bind();
			
		}
		, _preload: function(){
			_gf.spinStart();
		}
		, _beforeStart: function(){
			var thisClass = this;
			
			this._permitStart = {
				
			}
			
			this.q_seq = _util.getParam("q_seq");
			this.q_page = _util.getParam("q_page");
			
			
			$.ajax({
				url: _ROOT + "/js/mypage_common.js"
				, dataType: "script"
				, async: false
				, success: function(){
					
				}
				, error: function(){
					alert("error");
				}
			});
			
			_util.executeThread(function(){
				for(key in thisClass._permitStart){
					if(!thisClass._permitStart[key]) return false;
				}
				
				return (_gf.getMyInfoData());
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			// 목록, 수정, 삭제
			$("div#container").on("click", "img[alt='목록'], img[alt='수정'], img[alt='삭제']", function(){
				switch($(this).attr("alt")){
					case "목록":
						thisClass.goList();
						break;
					case "수정":
						thisClass.goModify();
						break;
					case "삭제":
						if(!confirm("삭제하시겠습니까?")) return;

						thisClass.articleDelete();
						break;
				}
			});
			
		}
		, _start: function(){
			// 내용
			this.objComm.getContents({
				"질문코드": this.q_seq
			});
		}
		// 내용완료
		, finishContents: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objQuestion.setInfo(ret["결과값"]);
			this.objAnswer.setInfo(ret["결과값"]);
		}
		// 삭제완료
		, finishArticleDelete: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.goList();
		}
		// 삭제
		, articleDelete: function(){
			_gf.spinStart("deleting...");
			
			this.objComm.articleDelete({
				"질문코드": this.q_seq
			});
		}
		, goList: function(){
			document.location.href = _ROOT + "/mypage/my_qna_list.html?q_page=" + this.q_page;
		}
		, goModify: function(){
			document.location.href = _ROOT + "/mypage/my_qna_write.html" + document.location.search;
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});
